using Microsoft.Extensions.Logging;
using Quartz;
using System;
using System.Threading.Tasks;
using Zhao.IServer.Events;
using Zhao.IServer.Field;


namespace Zhao.Arena.Utility.QuartZJob
{
    [DisallowConcurrentExecution]
    public class HistoricalOrderStatusJob : IJob
    {
        private readonly ILogger<HistoricalOrderStatusJob> _logger;
        private readonly IAppointmentsServer _appointmentsServer;
        private readonly IActivitiesServer _activitiesServer;

        public HistoricalOrderStatusJob(
            ILogger<HistoricalOrderStatusJob> logger,
            IAppointmentsServer appointmentsServer,
            IActivitiesServer activitiesServer)
        {
            _logger = logger;
            _appointmentsServer = appointmentsServer;
            _activitiesServer = activitiesServer;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                _logger.LogInformation("开始执行历史订单状态更新任务: {time}", DateTime.Now);
                
                // 更新历史场地预约订单状态
                int appointmentsUpdated = await _appointmentsServer.UpdateHistoricalAppointmentsStatusAsync();
                _logger.LogInformation("已更新 {count} 个历史场地预约订单状态", appointmentsUpdated);
                
                // 更新历史活动订单状态
                int activitiesUpdated = await _activitiesServer.UpdateHistoricalActivitiesStatusAsync();
                _logger.LogInformation("已更新 {count} 个历史活动订单状态", activitiesUpdated);
                
                _logger.LogInformation("历史订单状态更新任务完成: {time}", DateTime.Now);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "执行历史订单状态更新任务时发生错误");
            }
        }
    }
} 